<?php
/**
 * PHP 5
 *
 * MIT License
 * Copyright (c) 2009 Christian Krause
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * @category   library
 * @package    iLivGallery
 * @author     Original Author <ilivgallery@christiankrause.de>
 * @copyright  2009 Christian Krause
 * @license    MIT
 * @link       http://code.google.com/p/ilivgallery/downloads/
 * @since      File available since Release 0.8
 */

class iLiv_File extends SplFileInfo {

	protected $path;
	protected $name;
	protected $basename;
	protected $scale_down_name;
	protected $mime_by_suffix;
	protected $size;
	protected $mime;
	protected $width;
	protected $height;
	protected $bits;
	protected $channels;

	/**
	 * @param string $fileName
	 * @return iLiv_File
	 */
	public static function getInstance($fileName) {
		return new self($fileName);
	}

	public function getImageInfos() {

		$aFileDetails = getimagesize($this->getRealPath()); //width, height, mimetyp, bits, channels

		$this->path       = $this->getRealPath();
		$this->name       = $this->getFilename();
		$this->basename   = $this->getBasename($this->name);

		$this->mime_by_suffix = $this->getMimeTypeByExt();
		$this->size       = filesize($this->path);
		$this->width      = $aFileDetails[0];
		$this->height     = $aFileDetails[1];
		$this->bits       = $aFileDetails['bits'];
		$this->channels   = $aFileDetails['channels'];

		return $this;
	}

	public function getBasename() {
		$searchPattern = '/\.(?:jpg|jpeg|pdf|png)$/i';
		return preg_replace($searchPattern, '', $this->name, 1);
	}

	public function __get($name) {
		return $this->$name;
	}

	private function getMimeTypeByExt() {

		$ext = strToLower( substr( strrchr($this->name, "."),
								   1 )
						 ); // file-extension
		switch($ext)
		{
		/*
		 * Archive
		 */
		case "zip": $type = "application/zip";              break; //x-zip-compressed
		case "tar": $type = "application/x-tar";            break;
		case "sit": $type = "application/x-stuffit";        break;
		case "gtar":$type = "application/x-gtar";           break;
		case "hqx": $type = "application/mac-binhex40";     break;
		case "gz":  $type = "application/gzip";             break;
		case "tgz": $type  = "application/x-compressed";    break;
		case "rar": $type  = "application/x-compressed rar";break;
		/*
		 * images
		 */
		case "jpg": $type = "image/jpeg";                   break; //image/pjpeg
		case "jpeg":$type = "image/jpeg";                   break;
		case "gif": $type = "image/gif";                    break;
		case "png": $type = "image/png";                    break; //image/x-png
		case "tif": $type = "image/tiff";                   break;
		case "tiff":$type = "image/tiff";                   break;
		case "ai":  $type = "image/ai";                     break; //application/postscript
		case "eps": $type = "image/eps";                    break; //application/postscript
		case "psd": $type = "application/psd";              break;
		case "ico": $type = "image/x-icon";                 break;
		case "bmp": $type = "image/bmp";                    break;
		/*
		 * flash
		 */
		case "swf": $type = "application/x-shockwave-flash";break;
		/*
		 * web files
		 */
		case "php": $type = "application/x-httpd-php";      break;
		case "htm": $type = "text/html";                    break;
		case "html":$type = "text/html";                    break;
		case "xml": $type = "text/xml";                     break;
		case "xsl": $type = "text/xml";                     break;
		case "xslt":$type = "text/xml";                     break;
		case "js":  $type = "text/javascript";              break;
		case "css": $type = "text/css";                     break;
		case "svg": $type = "image/svg+xml";                break;
		/*
		 * Text / Office
		 */
		case "csv": $type = "text/comma-separated-values";  break;
		case "txt": $type = "text/plain";                   break;
		case "rtf": $type = "text/rtf";                     break;
		case "doc": $type = "application/msword";           break;
		case "dot": $type = "application/msword";           break;
		case "xls": $type = "application/msexcel";          break;
		case "pdf": $type = "application/pdf";              break;
		case "ppt": $type = "application/mspowerpoint";     break;
		case "pps": $type = "application/mspowerpoint";     break;
		case "vcf": $type = "text/x-vcard";                 break;
		/*
		 * Audio / Video
		 */
		case "ram": $type = "audio/x-pn-realaudio";         break;
		case "ra":  $type = "audio/x-pn-realaudio";         break;
		case "rm":  $type = "audio/x-pn-realaudio";         break;
		case "wav": $type = "audio/x-wav";                  break;
		case "aif": $type = "audio/x-aiff";                 break;
		case "aiff":$type = "audio/aiff";                   break;
		case "mp3": $type = "audio/mpeg";                   break;
		case "mpeg":$type = "video/mpeg";                   break;
		case "mpg": $type = "video/mpeg";                   break;
		case "mpe": $type = "video/mpeg";                   break;
		case "mp4": $type = "video/mp4";                    break;
		case "mov": $type = "video/quicktime";              break;
		case "qt":  $type = "video/quicktime";              break;
		case "avi": $type = "video/x-msvideo";              break;
		case "ogg": $type = "application/ogg";              break;
		case "wmv": $type = "video/x-ms-wmv";               break;
		case "flv": $type = "video/x-flv";                  break;

		default:    $type = "application/octet-stream";
		}

		return $type;
	}

	public function setScaleDown($scale) {

		$this->scale_down_name[$scale] = $this->basename.'_'.$scale.'.png';

		return $this->scale_down_name[$scale];
	}

}
